//
//  WinNumHistoryViewController.m
//  CP20180523
//
//  Created by apple on 2018/5/26.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "WinNumHistoryViewController.h"
#import "SearchResultViewController.h"


@interface WinNumHistoryViewController ()

@property (weak, nonatomic) IBOutlet UITextField *numberTextField;


@end

@implementation WinNumHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"号码历史";
}

#pragma mark - 判断数字
- (BOOL) deptNumInputShouldNumber:(NSString *)str
{
    if (str.length == 0) {
        return NO;
    }
    NSString *regex = @"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if ([pred evaluateWithObject:str]) {
        return YES;
    }
    return NO;
}


- (IBAction)searchAction:(id)sender {
    NSArray* numberArray = [self.numberTextField.text componentsSeparatedByString:@" "];
    if ([numberArray count]!=5) {
        [EasyTextView showErrorText:@"请按下方格式输入五个数字（01~11之间）"];
        return;
    }else{
        NSSet* checkSet = [[NSSet alloc] initWithArray:numberArray];
        if ([checkSet count]!=5) {
            [EasyTextView showErrorText:@"不要输入重复数字"];
            return;
        }
    }
    
    for (int i=0; i<[numberArray count]; i++) {
        NSString* objString = [numberArray objectAtIndex:i];
        if ([objString length]!=2) {
            [EasyTextView showErrorText:@"请按下方格式输入五个数字（01~11之间）"];
            return;
        }
        if (![self deptNumInputShouldNumber:objString]) {
            [EasyTextView showErrorText:@"请输入数字（01~11之间）"];
            return;
        }
        if ([objString integerValue]<=0||[objString integerValue]>11) {
            [EasyTextView showErrorText:@"请按下方格式输入五个数字（01~11之间）"];
            return;
        }

    }


    NSArray* checkArray = [numberArray sortedArrayUsingSelector:@selector(compare:)];
    NSString* checkString = [checkArray componentsJoinedByString:@"-"];

    NSInteger winCount = arc4random()%8;
    NSString* countString = [NSString stringWithFormat:@"在近2000次开奖中共开出%ld次",winCount];

    NSString* localCountString = [[NSUserDefaults standardUserDefaults] objectForKey:checkString];
    if ([localCountString length]==0) {
        localCountString = countString;
        [[NSUserDefaults standardUserDefaults] setObject:countString forKey:checkString];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
//    显示查询结果
    SearchResultViewController* viewController = [[UIStoryboard storyboardWithName:@"WinNumHistory" bundle:nil] instantiateViewControllerWithIdentifier:@"SearchResultViewController"];
    
    [viewController setPopinTransitionStyle:BKTPopinTransitionStyleCrossDissolve];
    [viewController setPopinTransitionDirection:BKTPopinTransitionDirectionTop];
    
    [viewController searchResultWith:@{@"kString":localCountString,@"kArray":numberArray} andCallBack:^(BOOL next) {
        [self.navigationController dismissCurrentPopinControllerAnimated:NO completion:^{
            
        }];
        if (next) {
            [SelectionNumManager shared].currentLotteryID = @"7";
            [SelectionNumManager shared].currentLotteryName = @"11选5";
            UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"SelectedNumber" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"Select11X5ViewController"];
            [self.navigationController pushViewController:acViewCon animated:YES];
        }
    }];
//    667
    CGFloat heightScale = self.view.frame.size.height/667.0;
    CGRect presentationRect = CGRectInset(self.view.bounds, 45, 190*heightScale);
    [self.navigationController presentPopinController:viewController fromRect:presentationRect animated:YES completion:^{
        NSLog(@"Popin presented !");
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
